package nf.action;

import nf.dao.CellPhoneDao;
import nf.dao.CellPhoneDaoImpl;
import nf.entity.CellPhone;
import nf.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/userServlet")
public class userServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //配置字符编码为UTF-8
        request.setCharacterEncoding("utf-8");
        //获取客户端传递过来的信息
        String username = request.getParameter("uesrname");
        String password = request.getParameter("password");
        //new一个dao对象操作数据库
        CellPhoneDao dao = new CellPhoneDaoImpl();
        User a = new User();
        a.setUserName(username);
        a.setPassword(password);

        User b = dao.select(a);

         if(b!=null){
             HttpSession session = request.getSession();
             session.setAttribute("username",b.getUserName());
             session.setAttribute("password",b.getPassword());
             response.sendRedirect("ServletPhone");
         }else{
             //request.setAttribute("sb","用户名或者密码错误");
             //request.getRequestDispatcher("index.jsp").forward(request,response);
             response.sendRedirect("index.jsp");
             //PrintWriter p = response.getWriter();
             //p.equals()
         }


    }
}
